package com.hnkywl.sts.service.report;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.organ.EjzhDao;
import com.hnkywl.sts.dao.organ.GsjgDao;
import com.hnkywl.sts.dao.report.EjzhmxDao;
import com.hnkywl.sts.entity.organ.Ejzh;
import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.entity.zjgl.Bzj;
import com.hnkywl.sts.service.sys.YgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 14-1-17
 * Time: 上午9:44
 * To change this template use File | Settings | File Templates.
 */
@Service
public class EjzhrjService {

    private EjzhmxDao ejzhmxDao;

    public String findEjzhrj(Long gsId, Long khId, Date endDate, int pageNo, int pageSize) {
        Date nowDate = endDate;
        Calendar c = Calendar.getInstance();
        c.setTime(endDate);
        c.add(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Map result = ejzhmxDao.zjrjList(gsId, khId, nowDate,c.getTime(), pageNo, pageSize);
        return "{\"total\":" + result.get("total") + ",\"rows\":" + new Gson().toJson(result.get("rows")) + "}";
    }

    @Transactional
    public void rjEjzhrj(Long gsId,Date date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        if(!this.ejzhmxDao.checkZjrj(gsId,date)){
            throw new RuntimeException("该公司在"+df.format(date)+"已经日结");
        }
       this.ejzhmxDao.saveZjrj(gsId,date, YgService.getCurrentYg().getId());
    }

    @Transactional
    public void deleteEjzhrj(Long gsId,Date date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        if(this.ejzhmxDao.checkZjrj(gsId,date)){
            throw new RuntimeException("该公司在"+df.format(date)+"未日结");
        }
        this.ejzhmxDao.deleteZjrj(gsId,date);
    }




    @Autowired
    public void setEjzhmxDao(EjzhmxDao ejzhmxDao) {
        this.ejzhmxDao = ejzhmxDao;
    }
}
